package 知识点复习.异步回调.异步任务的串行执行;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.function.Supplier;

//调用thenApply分两步计算(10+10)*2
public class ThenApplyDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Long> future = CompletableFuture.supplyAsync(new Supplier<Long>() {
            @Override
            public Long get() {
                long firstStep = 10L + 10L;
                System.out.println(Thread.currentThread().getName() + ":firstStep out is " + firstStep);
                return firstStep;
            }
        }).thenApplyAsync(new Function<Long, Long>() {
            @Override
            public Long apply(Long firstStepOut) {
                long secondStep = firstStepOut * 2;
                System.out.println(Thread.currentThread().getName() + ":secondStep out is " + secondStep);
                return secondStep;
            }
        });

        Long result = future.get();
        System.out.println(Thread.currentThread().getName() + ":out is " + result);
    }
}
